package br.com.future.study.pool;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID;

import br.com.future.study.jdbc.ConnectionPool;

/**
 * This class shows an example worker thread that can be used with the thread pool. It demonstrates the main points that
 * should be included in any worker thread. Use this as a starting point for your own threads.
 * 
 * @author Jeff Heaton (http://www.jeffheaton.com)
 * @version 1.0
 */
public class Worker implements Runnable {

	static private int count = 0;
	private int taskNumber;
	protected Done done;
	private int iterations;

	/**
	 * @param done
	 */
	Worker(int iterationsQnt) {
		this.iterations = iterationsQnt;
		count++;
		taskNumber = count;
	}

	public void run() {
		Connection conn = null;
		PreparedStatement preparedStatement = null;
		ResultSet rs = null;
		System.out.println("Creating connection.");
		try {
			conn = ConnectionPool.getConnection();
			System.out.println("Creating statement.");
			//final String query = "select * from future.user where email = ? limit 100000";
			final String query = "insert into future.user (name, email) values (?,?)";
			preparedStatement = conn.prepareStatement(query);
			double time;
			for (int i = 0; i < iterations; i++) {
				
				time = System.currentTimeMillis();
				preparedStatement.setString(1, UUID.randomUUID().toString());
				preparedStatement.setString(2, "rafa.ferreira@gmail.com");

				//System.out.println("Executing statement.");
				//System.out.println("Executing insert...");
				preparedStatement.execute();
				//System.out.println(preparedStatement.getUpdateCount());


				//				while (rs.next()) {
				//					for (int j = 1; j <= numcols; j++) {
				//						System.out.print("\t" + rs.getString(j));
				//					}
				//					System.out.println("");
				//				}
				time = System.currentTimeMillis() - time;
				//System.out.println(time);
				WorkerInfo workerInfo = new WorkerInfo();
				workerInfo.setThreadName("I'm thread #" + taskNumber);
				workerInfo.setTime(time);
				MetricsSingleton.getInstance().addMetrics(workerInfo);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null)
					rs.close();
			} catch (Exception e) {
			}
			try {
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (Exception e) {
			}
			try {
				if (conn != null)
					conn.close();
			} catch (Exception e) {
			}
		}
	}


	//	public void run() {
	//		ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
	//		SayHello hello = (SayHello) context.getBean("hello");
	//		double time;
	//		for (int i = 0; i < iterations; i++) {
	//			
	//			time = System.currentTimeMillis();
	//			//hello.greet();
	//			hello.addUser();
	//			//hello.printUsers();
	//			time = System.currentTimeMillis() - time;
	//			//System.out.println(time);
	//			WorkerInfo workerInfo = new WorkerInfo();
	//			workerInfo.setThreadName("I'm thread #" + taskNumber);
	//			workerInfo.setTime(time);
	//			MetricsSingleton.getInstance().addMetrics(workerInfo);
	//		}
	//	}
}
